home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 April / Chip CMCD0400.iso / SOFTWARE / Freeware / Programare / Bass / BASSTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-24  |  8.4 KB  |  315 lines

  1. /* BASS Simple Test (rev.8), copyright (c) 1999 Ian Luck.
  2. =========================================================
  3. Other source: basstest.rc
  4. Imports: bass.lib, kernel32.lib, user32.lib, comdlg32.lib
  5. */
  6.  
  7. #include <windows.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "bass.h"
  11. #include "basstest.h"
  12.  
  13. static HWND win=NULL;
  14. static HINSTANCE inst;
  15.  
  16. static HMUSIC *mods=NULL;
  17. static int modc=0;
  18. static HSAMPLE *sams=NULL;
  19. static int samc=0;
  20. static HSTREAM str;
  21. static BOOL cdplaying=FALSE;
  22.  
  23. /* Display error dialogs */
  24. static void Error(char *es)
  25. {
  26.     char mes[200];
  27.     sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
  28.     MessageBox(win,mes,"Error",0);
  29. }
  30.  
  31. /* Messaging macros */
  32. #define MESS(id,m,w,l) SendMessage(GetDlgItem(h,id),m,(WPARAM)w,(LPARAM)l)
  33. #define MLM(m,w,l) MESS(ID_MODLIST,m,w,l)
  34. #define SLM(m,w,l) MESS(ID_SAMLIST,m,w,l)
  35. #define GETMOD() MLM(LB_GETCURSEL,0,0)
  36. #define GETSAM() SLM(LB_GETCURSEL,0,0)
  37.  
  38. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  39. {
  40.     static OPENFILENAME ofn;
  41.     static char path[MAX_PATH];
  42.  
  43.     switch (m) {
  44.         case WM_TIMER:
  45.             {
  46.                 char text[10];
  47.                 int p;
  48.                 /* update the CD status */
  49.                 MESS(ID_CDINDRIVE,BM_SETCHECK,BASS_CDInDrive(),0);
  50.                 p=BASS_ChannelGetPosition(CDCHANNEL);
  51.                 sprintf(text,"%d:%02d",p/60000,(p/1000)%60);
  52.                 MESS(ID_CDPOS,WM_SETTEXT,0,text);
  53.                 /* update the CPU usage % display */
  54.                 _itoa(BASS_GetCPU(),text,10);
  55.                 MESS(ID_CPU,WM_SETTEXT,0,text);
  56.                 /* update the volume level display */
  57.                 _itoa(BASS_GetVolume(),text,10);
  58.                 MESS(ID_VOLUME,WM_SETTEXT,0,text);
  59.             }
  60.             break;
  61.  
  62.         case WM_COMMAND:
  63.             switch (LOWORD(w)) {
  64.                 case IDCANCEL:
  65.                     DestroyWindow(h);
  66.                     return 1;
  67.                 case ID_MODADD:
  68.                     {
  69.                         char file[MAX_PATH]="";
  70.                         HMUSIC mod;
  71.                         ofn.lpstrFilter="MOD music files (xm/mod/s3m/it/mtm)\0*.xm;*.mod;*.s3m;*.it;*.mtm\0All files\0*.*\0\0";
  72.                         ofn.lpstrFile=file;
  73.                         if (GetOpenFileName(&ofn)) {
  74.                             memcpy(path,file,ofn.nFileOffset);
  75.                             path[ofn.nFileOffset-1]=0;
  76.                             /* Load a music from "file" and make it use ramping */
  77.                             if (mod=BASS_MusicLoad(FALSE,file,0,0,BASS_MUSIC_RAMP)) {
  78.                                 modc++;
  79.                                 mods=(HMUSIC*)realloc((void*)mods,modc*sizeof(HMUSIC));
  80.                                 mods[modc-1]=mod;
  81.                                 MLM(LB_ADDSTRING,0,file);
  82.                             } else
  83.                                 Error("Can't load music");
  84.                         }
  85.                     }
  86.                     return 1;
  87.                 case ID_MODREMOVE:
  88.                     {
  89.                         int s=GETMOD();
  90.                         if (s!=LB_ERR) {
  91.                             MLM(LB_DELETESTRING,s,0);
  92.                             /* Free the music from memory */
  93.                             BASS_MusicFree(mods[s]);
  94.                             memcpy(mods+s,mods+s+1,(modc-s-1)*sizeof(HMUSIC));
  95.                             modc--;
  96.                         }
  97.                     }
  98.                     return 1;
  99.                 case ID_MODPLAY:
  100.                     {
  101.                         int s=GETMOD();
  102.                         /* Play the music (continue from current position) */
  103.                         if (s!=LB_ERR)
  104.                             if (!BASS_MusicPlay(mods[s])) Error("Can't play music");
  105.                     }
  106.                     return 1;
  107.                 case ID_MODSTOP:
  108.                     {
  109.                         int s=GETMOD();
  110.                         /* Stop the music */
  111.                         if (s!=LB_ERR) BASS_ChannelStop(mods[s]);
  112.                     }
  113.                     return 1;
  114.                 case ID_MODRESTART:
  115.                     {
  116.                         int s=GETMOD();
  117.                         /* Play the music from the start */
  118.                         if (s!=LB_ERR) BASS_MusicPlayEx(mods[s],0,-1,TRUE);
  119.                     }
  120.                     return 1;
  121.  
  122.                 case ID_SAMADD:
  123.                     {
  124.                         char file[MAX_PATH]="";
  125.                         HSAMPLE sam;
  126.                         ofn.lpstrFilter="WAVE sample files (wav)\0*.wav\0All files\0*.*\0\0";
  127.                         ofn.lpstrFile=file;
  128.                         if (GetOpenFileName(&ofn)) {
  129.                             memcpy(path,file,ofn.nFileOffset);
  130.                             path[ofn.nFileOffset-1]=0;
  131.                             /* Load a sample from "file" and give it a max of 3 simultaneous
  132.                             playings using playback position as override decider */
  133.                             if (sam=BASS_SampleLoad(FALSE,file,0,0,3,BASS_SAMPLE_OVER_POS)) {
  134.                                 samc++;
  135.                                 sams=(HSAMPLE*)realloc((void*)sams,samc*sizeof(HSAMPLE));
  136.                                 sams[samc-1]=sam;
  137.                                 SLM(LB_ADDSTRING,0,file);
  138.                             } else
  139.                                 Error("Can't load sample");
  140.                         }
  141.                     }
  142.                     return 1;
  143.                 case ID_SAMREMOVE:
  144.                     {
  145.                         int s=GETSAM();
  146.                         if (s!=LB_ERR) {
  147.                             SLM(LB_DELETESTRING,s,0);
  148.                             /* Free the sample from memory */
  149.                             BASS_SampleFree(sams[s]);
  150.                             memcpy(sams+s,sams+s+1,(samc-s-1)*sizeof(HSAMPLE));
  151.                             samc--;
  152.                         }
  153.                     }
  154.                     return 1;
  155.                 case ID_SAMPLAY:
  156.                     {
  157.                         int s=GETSAM();
  158.                         /* Play the sample from the start, volume=50, random pan position,
  159.                         using the default frequency and looping settings */
  160.                         if (s!=LB_ERR) {
  161.                             if (!BASS_SamplePlayEx(sams[s],0,-1,50,(rand()%201)-100,-1))
  162.                                 Error("Can't play sample");
  163.                         }
  164.                     }
  165.                     return 1;
  166.  
  167.                 case ID_STRNEW:
  168.                     {
  169.                         char file[MAX_PATH]="";
  170.                         ofn.lpstrFilter="Streamable files (wav/mp3)\0*.wav;*.mp3\0All files\0*.*\0\0";
  171.                         ofn.lpstrFile=file;
  172.                         if (GetOpenFileName(&ofn)) {
  173.                             memcpy(path,file,ofn.nFileOffset);
  174.                             path[ofn.nFileOffset-1]=0;
  175.                             /* Free old stream (if any) and create new one */
  176.                             BASS_StreamFree(str);
  177.                             MESS(ID_STRFILE,WM_SETTEXT,0,"");
  178.                             if (!(str=BASS_StreamCreateFile(FALSE,file,0,0,0)))
  179.                                 Error("Can't create stream");
  180.                             else
  181.                                 MESS(ID_STRFILE,WM_SETTEXT,0,file);
  182.                         }
  183.                     }
  184.                     return 1;
  185.                 case ID_STRPLAY:
  186.                     /* Play stream, not flushed */
  187.                     if (!BASS_StreamPlay(str,FALSE,0))
  188.                         Error("Can't play stream");
  189.                     return 1;
  190.                 case ID_STRSTOP:
  191.                     /* Stop the stream */
  192.                     BASS_ChannelStop(str);
  193.                     return 1;
  194.  
  195.                 case ID_CDPLAY:
  196.                     /* Play CD track (looped) */
  197.                     {
  198.                         char track[4];
  199.                         MESS(ID_CDTRACK,WM_GETTEXT,4,track);
  200.                         if (!BASS_CDPlay(atoi(track),TRUE))
  201.                             Error("Can't play CD");
  202.                         else
  203.                             cdplaying=TRUE;
  204.                     }
  205.                     return 1;
  206.                 case ID_CDSTOP:
  207.                     /* Pause CD */
  208.                     BASS_ChannelPause(CDCHANNEL);
  209.                     cdplaying=FALSE;
  210.                     return 1;
  211.                 case ID_CDRESUME:
  212.                     /* Resume CD */
  213.                     if (BASS_ChannelResume(CDCHANNEL))
  214.                         cdplaying=TRUE;
  215.                     return 1;
  216.  
  217.                 case ID_STOP:
  218.                     /* Pause digital output and CD */
  219.                     BASS_Pause();
  220.                     BASS_ChannelPause(CDCHANNEL);
  221.                     return 1;
  222.                 case ID_RESUME:
  223.                     /* Resume digital output and CD */
  224.                     if (cdplaying) BASS_ChannelResume(CDCHANNEL);
  225.                     BASS_Start();
  226.                     return 1;
  227.                 case ID_FADE:
  228.                     /* Check the digital sound volume */
  229.                     if (BASS_GetVolume()) {
  230.                         /* If not silent, fade-out (digital and CD) */
  231.                         if (!BASS_SlideVolume(0,20,BASS_SLIDE_DIGITAL|BASS_SLIDE_CD))
  232.                             Error("Can't slide volume");
  233.                     } else
  234.                         /* ... else, fade-in (digital and CD) */
  235.                         BASS_SlideVolume(100,20,BASS_SLIDE_DIGITAL|BASS_SLIDE_CD);
  236.                     return 1;
  237.             }
  238.             break;
  239.  
  240.         case WM_INITDIALOG:
  241.             MESS(ID_CDTRACK,WM_SETTEXT,0,"1");
  242.             SetTimer(h,1,250,NULL);
  243.             GetCurrentDirectory(MAX_PATH,path);
  244.             memset(&ofn,0,sizeof(ofn));
  245.             ofn.lStructSize=sizeof(ofn);
  246.             ofn.hwndOwner=h;
  247.             ofn.hInstance=inst;
  248.             ofn.nMaxFile=MAX_PATH;
  249.             ofn.lpstrInitialDir=path;
  250.             ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  251.             return 1;
  252.  
  253.         case WM_DESTROY:
  254.             KillTimer(h,1);
  255.             PostQuitMessage(0);
  256.             return 1;
  257.     }
  258.     return 0;
  259. }
  260.  
  261. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  262. {
  263.     MSG msg;
  264.     inst=hInstance;
  265.  
  266. /* Check that BASS 0.6 was loaded */
  267.     if (BASS_GetVersion()!=MAKELONG(0,6)) {
  268.         Error("BASS version 0.6 was not loaded");
  269.         return 0;
  270.     }
  271.  
  272.     if (!(win=CreateDialog(hInstance,MAKEINTRESOURCE(1000),NULL,&dialogproc))) {
  273.         Error("Can't create window");
  274.         return 0;
  275.     }
  276.  
  277. /* Initialize digital sound - default device, 44100hz, stereo, 16 bits */
  278.     if (!BASS_Init(-1,44100,0,win)) Error("Can't initialize digital sound system");
  279. /* Initialize CD */
  280.     if (!BASS_CDInit(NULL)) Error("Can't initialize CD system");
  281.  
  282.     BASS_Start();    /* Start digital output */
  283.  
  284.     while (1) {
  285.         if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  286.             if (!GetMessage(&msg, NULL, 0, 0))
  287.                 break;
  288.             TranslateMessage(&msg);
  289.             DispatchMessage(&msg);
  290.         } else
  291.             WaitMessage();
  292.     }
  293.     
  294.     BASS_Stop();    /* Stop digital output */
  295. /* Free the stream */
  296.     BASS_StreamFree(str);
  297. /* It's not actually necessary to free the musics and samples
  298. because they are automatically freed by BASS_Free() */
  299. /* Free musics */
  300.     if (mods) {
  301.         int a;
  302.         for (a=0;a<modc;a++) BASS_MusicFree(mods[a]);
  303.         free(mods);
  304.     }
  305. /* Free samples */
  306.     if (sams) {
  307.         int a;
  308.         for (a=0;a<samc;a++) BASS_SampleFree(sams[a]);
  309.         free(sams);
  310.     }
  311.     BASS_Free();    /* Close digital sound system */
  312.     BASS_CDFree();    /* Close CD system */
  313.     return 0;
  314. }
  315.